﻿using DevExpress.XtraEditors;
using Lamex.Library.CommonClass;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Calculate
{
    public class LGC_TIM_VTA
    {
        public static double LGC_TIM_VTA_VALUE(string DWG_NO,string PART_NO ,string FeatureName,string FeartureValue, double V)
        {
            string errorMessage = DWG_NO + ";" + PART_NO + ";" + FeatureName;
            try
            {
                double LGC_TIM_VTA_VALUE = 0;
                double T = 0;
                double A = 0;
                string unitWorks = "";
                string attackWorks = "";
//                DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(@"select   a.DWG_NO,a.PART_NO,a.FEATURE_NAME,a.PROCESS_CODE,c.PROD_CATEGORY,b.UNIT,b.FEAT_VALUE_FROM,b.UNIT_WORKHOURS,b.ATTACH_WORKHOURS,a.FEAT_VALUE,b.FEAT_VALUE_FROM,b.VALUE_LOGIC,b.WORKTIME_LOGIC_CODE from [dbo].[IE_Part_FeatureList] a 
//                left join[dbo].[IE_Feature_Definatian]  b on b.FEATURE_NAME = a.FEATURE_NAME
//                left join[dbo].[IE_Part_TaskList] c on c.PART_NO = a.PART_NO
//                WHERE  a.PART_NO='"+ PART_NO + "'  and a.FEATURE_NAME='"+ FeatureName + "' ");
                DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(@"SELECT  [FEATURE_NAME]
      ,[DISPLAY_SEQUENCE]
      ,[UNIT_WORKHOURS]
      ,[ATTACH_WORKHOURS]
      ,[UNIT]
      ,[FEAT_VALUE_FROM]
      ,[VALUE_LOGIC]
      ,[WORKTIME_LOGIC_CODE]
      ,[FEATURE_DESC] 
  FROM [LamexMESManager].[dbo].[IE_Feature_Definatian]
               WHERE  FEATURE_NAME='" + FeatureName + "'");
                if (dt.Rows.Count == 0)
                {
                   // XtraMessageBox.Show("【特征定义表没有该特征,请确保特征定义表存有该特征！】");
                    return 0;
                }
                            
                if (dt.Rows[0]["UNIT_WORKHOURS"] != null)
                {
                    //计算单位工时
                    if (dt.Rows[0]["UNIT_WORKHOURS"].ToString() == "LGC_UNT_LABOR")
                    {
                        unitWorks = LGC_UNT_LABOR.LGC_UNT_LABOR_VALUE(dt.Rows[0]["FEATURE_NAME"].ToString(), FeartureValue).ToString();
                    }
                    else if (dt.Rows[0]["UNIT_WORKHOURS"].ToString() == "LGC_UNT_P31_SAW")
                    {
                        unitWorks = LGC_UNT_P31_SAW.LGC_UNT_P31_SAW_VALUE(PART_NO).ToString();
                    }
                    else if (dt.Rows[0]["UNIT_WORKHOURS"].ToString().Contains("?"))
                    {
                        unitWorks = IELogicHelper.GetLogicValue(DWG_NO, PART_NO, dt.Rows[0]["UNIT_WORKHOURS"].ToString().Trim());

                    }
                    else if (dt.Rows[0]["UNIT_WORKHOURS"].ToString().Contains("NIL"))
                    {
                        unitWorks = "0";
                    }
                    else if (dt.Rows[0]["UNIT_WORKHOURS"].ToString()=="")
                    {
                        unitWorks = "0";
                    }
                    else
                        unitWorks = dt.Rows[0]["UNIT_WORKHOURS"].ToString();

                   
                    T = double.Parse(unitWorks);
                }
                else
                    T = 0;

                if (dt.Rows[0]["ATTACH_WORKHOURS"] != null)
                {
                    //计算附加工时
                    if (dt.Rows[0]["ATTACH_WORKHOURS"].ToString().Contains("?"))
                    {

                        attackWorks = IELogicHelper.GetLogicValue(DWG_NO, PART_NO, dt.Rows[0]["ATTACH_WORKHOURS"].ToString().Trim());

                    }
                    else if (dt.Rows[0]["ATTACH_WORKHOURS"].ToString().Contains("NIL"))
                    {
                        attackWorks = "NIL";
                    }
                    else if(dt.Rows[0]["ATTACH_WORKHOURS"].ToString()=="")
                    {
                        attackWorks = "0";
                    }
                    else
                    {
                        attackWorks = dt.Rows[0]["ATTACH_WORKHOURS"].ToString();
                    }

                    A = double.Parse(attackWorks);
                }  
                else
                    A = 0;
                if (V == 900000 || V == 900001 ||  V == 900002 ||  V == 900003 
                    || V == 900004 || V == 900005  || V == 900006 || V == 900007 
                    || V == 900008 || V == 900009)
                {
                    return V;
                }
                if (T == 900000 || T == 900001 || T == 900002 || T == 900003
                    || T == 900004 || T == 900005 || T == 900006 || T == 900007
                    || T == 900008 || T == 900009)
                {
                    return T;
                }
                if (A == 900000 || A == 900001 || A == 900002 || A == 900003
                  || A == 900004 || A == 900005 || A == 900006 || A == 900007
                  || A == 900008 || A == 900009)
                {
                    return A;
                }
                if (V == 0)
                    LGC_TIM_VTA_VALUE = 0;
                else
                    LGC_TIM_VTA_VALUE = V * T + A;
                return LGC_TIM_VTA_VALUE;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message+":"+ errorMessage);
                return 0;
            }

        }

        public static double LGC_TIM_VTA02_VALUE(string DWG_NO, string PART_NO,string FeatureName, string Ts, string As, double Vs)
        {
            string errorMessage = DWG_NO + ";" + PART_NO + ";" + FeatureName;
            try
            {
                double T = double.Parse(Ts);
                double A = double.Parse(As); ;
                double V = Vs;
                double LGC_TIM_VTA_VALUE = 0;
                if (V == 0)
                    LGC_TIM_VTA_VALUE = 0;
                else
                    LGC_TIM_VTA_VALUE = V * T + A;
                return LGC_TIM_VTA_VALUE;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message + ":" + errorMessage);
                return 0;
            }
        }
    }
}
